package com.darren.euc.dao.impl;


import java.math.BigInteger;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.darren.euc.dao.UserDao;
import com.darren.euc.entity.User;


@Repository
public class UserDaoImpl extends BaseDaoImpl<User, Integer> implements UserDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getAll() {
		Session session = sessionFactory.getCurrentSession();	
		Query query  = session.createQuery(" from User where superUser <> '1' and enable = true ");
		return query.list();
	}

	@Override
	public User getUserByUserName(String userName) {
		Session session = sessionFactory.getCurrentSession();	
		Query query = session.createQuery("from User as a where (a.userName = :username or a.mobile = :mobile or a.wechat = :wechat) and enable = true ");
		query.setParameter("username", userName);
		query.setParameter("mobile", userName);
		query.setParameter("wechat", userName);
		return (User) query.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUserByMobile(String mobile) {
		Session session = sessionFactory.getCurrentSession();	
		Query query = session.createQuery("from User as a where a.mobile = :mobile and enable = true ");
		query.setParameter("mobile", mobile);
		return query.list();
	}
	
	@Override
	public boolean IsAdmin(Integer userId,Integer functionId){
		Session session = sessionFactory.getCurrentSession();	
		Query query = session.createSQLQuery("SELECT count(1) from euc_user_role role INNER JOIN euc_role_admin admin on role.role_id = admin.role_id where admin.admin = :functionId and role.user_id = :userId ");
		query.setParameter("userId", userId);
		query.setParameter("functionId", functionId);
		BigInteger result = (BigInteger) query.uniqueResult();		
		return !result.equals(new BigInteger("0"));
	}			
	
	@SuppressWarnings("unchecked")
	@Override
    public List<User> getAdminListByFunctionId(String functionId){
		Session session = sessionFactory.getCurrentSession();	
		Query query = session.createSQLQuery("SELECT * from euc_users where user_id in (SELECT user_id from euc_user_role role INNER JOIN euc_role_admin admin on role.role_id = admin.role_id where admin.admin = :functionId)").addEntity(User.class);
		query.setParameter("functionId", functionId);
		return query.list();
	}
	
	
	
	
	private static String sqlGetUserByFunction = "SELECT	* " +
												"	FROM	euc_users " +
												"	WHERE (user_id IN (SELECT DISTINCT	userrole.user_id " +
												"					FROM	euc_functions fun " +
												"					INNER JOIN euc_role_function rolefun ON rolefun.function_id = fun.id " +
												"					INNER JOIN euc_roles roles ON roles.id = rolefun.role_id " +
												"					INNER JOIN euc_user_role userrole ON roles.id = userrole.role_id " +
												"					WHERE	fun.function_id = ?)  " +
												"	and exists(select 1 from euc_users as users  " +
												"									INNER JOIN euc_user_role as userrole on users.user_id = userrole.user_id " +
												"									INNER JOIN euc_role_admin as admin on admin.role_id = userrole.role_id and admin.admin = '10000' " +
												//"									where users.user_id = ?)) " +
												"									 )) " +
												"	or user_id = ?;";
	
	@SuppressWarnings("unchecked")
	@Override
    public List<User> getUserListByFunctionId(String functionId,Integer userId){
		Session session = sessionFactory.getCurrentSession();	
		Query query = session.createSQLQuery(sqlGetUserByFunction).addEntity(User.class);
		query.setParameter(0, functionId);
		query.setParameter(1, userId);
//		query.setParameter(2, userId);
		return query.list();
	}

}